<?php
namespace \Absidon\Controller\Uri;
/**
 * Pathinfo URI class for parsing the path from a query var.
 *
 * The pathinfo URI structure has index.php and allows for mod_rewrite like URLs for systems that
 * don't support mod_rewrite. The URI does not look as pretty as mod_rewrite, but does allow for
 * some SEO, instead of only using query vars.
 *
 * Example: index.php?/controller/action&query=var
 * Example: index.php?/controller/action/subcontroller/subaction
 *
 * The only reason this is broken out from the mod_rewrite URI class is for URI building purposes.
 * This class should not be used to directly build URI, that is contained in a separate helper
 * component that will depend on the URI classes.
 *
 * @author Jacob Santos <webmaster@absidongames.com>
 * @since 0.1
 * @package Absidon
 * @subpackage Controller_Uri
 * @license Simplified BSD License <http://www.opensource.org/licenses/bsd-license.html>
 * @license GNU Lesser General Public License 2.1 or later <http://www.opensource.org/licenses/lgpl-2.1.php>
 */

/**
 * Pathinfo URI class for parsing the path from a query var.
 *
 * The class tests to see if the URI structure appears to be using Pathinfo. It does this by
 * checking for index.php and a directory structure in the URI. By default, the router will attempt
 * to auto find the correct class and does so by first calling each classes {@link Pathinfo::test()}
 * method.
 *
 * The class will match against the PathInfo structure for a location and return either true or an
 * array for options based on the match.
 *
 * This class is not for calling controllers, that is the routers job. This class should also not be
 * used by the application as it is an internal library component.
 */
class Pathinfo implements Uri_Component, Pretty_Uri_Component
{
	/**
	 * Whether the pathinfo is being used using a query var.
	 *
	 * It appears that pathinfo is actually part of the index.php, but is not as common as having
	 * the path as a query var. It is also just to have it as a query var, as not all apache and
	 * PHP versions support having the pathinfo as index.php/stuff/whatever. While the following:
	 * index.php?/stuff/whatever will always work on every HTTP server.
	 *
	 * The {@link Absidon\Controller\Uri\rewrite()} class supports true pathinfo.
	 *
	 * @since 0.1
	 * @see Absidon\Controller\Uri\uri_component::is_used() Implements.
	 *
	 * @return boolean
	 */
	public function is_used()
	{
		if( preg_match('/[a-z]+\.php\?\/.*?\/?/i', get_uri_request()) )
		{
			// Ensure that we get this right. This extra check is probably not needed, but use
			// anyway to bar any faulty URIs that don't include this correctly or in case we
			// incorrectly identify query vars as pathinfo.
			if( ! isset($_GET[0]) )
			{
				return false;
			}

			$pathinfo = $_GET[0];

			// We first check that the first character is part of a directory and make sure that the
			// URI includes an actual path, albeit, it could be a single character ('/a'). Possible,
			// but not likely.
			if( $pathinfo{1} != '/' || strlen($pathinfo) == 1 )
			{
				return false;
			}

			return true;
		}

		return false;
	}

	/**
	 * @since 0.1
	 * @see Absidon\Controller\Uri\uri_component::process_uri() Implements.
	 */
	public function process_uri()
	{
		//
	}

	/**
	 * @since 0.1
	 * @see Absidon\Controller\Uri\pretty_uri_component::match() Implements.
	 *
	 * @param string $regex
	 * @returns bool|array
	 */
	public function match($regex)
	{
		/** @todo complete. */
	}
}